package TrafficSim;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Vector;

public class VehicleLists {
	
	private Master master;
	private List<Vehicle> vehicleList;
	private List<Vehicle> cloneList;
	private List<Vehicle> taskList;
	private List<Vehicle> allVehicles;
	private boolean taskListEmpty = false;
	
	public List<Vehicle> getTaskList() {
		return taskList;
	}

	private int numberOfVehicles = 0;
	
	public int getNumberOfVehicles() {
		return numberOfVehicles;
	}


	public VehicleLists(Master master){
		this.master = master;
		vehicleList = new ArrayList<Vehicle>();
		vehicleList = Collections.synchronizedList(new ArrayList<Vehicle>());
		cloneList = new ArrayList<Vehicle>();
		cloneList = Collections.synchronizedList(new ArrayList<Vehicle>());
		taskList = new ArrayList<Vehicle>();
		taskList = Collections.synchronizedList(new ArrayList<Vehicle>());
		this.allVehicles = new ArrayList<Vehicle>();
		this.allVehicles = Collections.synchronizedList(new ArrayList<Vehicle>());
	}
	
	public void addVehicle (Vehicle vehicle){
		vehicleList.add(vehicle);
		this.allVehicles.add(vehicle);
		numberOfVehicles++;
	}
	

	public List<Vehicle> getAllVehicles() {
		return allVehicles;
	}


	public void fillTasklist() {

//		System.out.println("filling");
			for (Vehicle v : vehicleList){
				v.switchPos();
			}
			taskList = vehicleList;
//			taskList = (Vector<Vehicle>) Cloner.cloneObject(vehicleList);

		
	}

	public List<Vehicle> getCloneList() {
		return cloneList;
	}

	public List<Vehicle> getVehicleList() {
		return vehicleList;
	}

	public synchronized void refreshList() {
		
		vehicleList = cloneList;
		cloneList = new Vector<Vehicle>();
		this.taskListEmpty = false;
//		System.out.println("Tasklist auf false gesetzt");

	}

	public synchronized Vehicle getTask() {
		if (this.taskListEmpty){
//			System.out.println("Liste leer");
			return null;
		}
		if (this.taskList.size() == 1){
//			System.out.println(this.numberOfVehicles);
			this.taskListEmpty = true;
		}
//		System.out.println(this.taskListEmpty);
		if (this.taskList.size() == 0){
			return null;
		}
		return taskList.remove(0);

	}

	public synchronized boolean  insertInCloneList(Vehicle vehicle) {
//		System.out.println("Auto in Liste");
//		System.out.println("cloneList " +cloneList.size());
		cloneList.add(vehicle);
//		System.out.println("nachher cloneList " +cloneList.size());
//		System.out.println("Anzahl vehicles " +numberOfVehicles);

		if (cloneList.size() == numberOfVehicles) {

			synchronized (master) {
//				System.out.println("Master aufgeweckt");
				master.notify();

				return true;
			}
		}
		return false;
	}

	public synchronized boolean isEmpty() {
		return taskList.isEmpty();
	}


	public void destroyVehicle() {
		
		this.numberOfVehicles -= 1;		
		if (cloneList.size() == numberOfVehicles){
			synchronized(master){
				master.notify();
			}
		}
	}



}
